package com.ebupt.migu.music.api.util;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.music.common.constants.RedidKeyConstant;
import com.ebupt.migu.music.roster.entity.RosterBlackEntity;
import com.ebupt.migu.music.roster.entity.RosterWhiteEntity;
import com.ebupt.migu.music.roster.mapper.RosterMapper;
import com.ebupt.migu.music.userGroup.entity.bo.UserGroupBo;
import com.ebupt.migu.music.userGroup.mapper.CrowdMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author xuhongsong
 * @version 1.0
 * @date 2023/3/30 18:00
 */
@EnableScheduling
@Component
@Slf4j
public class CacheTask {

    @Resource
    private RosterMapper rosterMapper;

    @Resource
    private CrowdMapper crowdMapper;
    //  缓存放入
    @Resource
    private RedisTemplate redisTemplate;

    public static Long cacheTime = 1000 * 60 * 60L;

    /**
     * 根据效果统计更改多文案默认配置
     */
    @Async
    @Scheduled(cron = "${cron.cache_update}")
    public void updateCache() {
        log.info("[初始化黑白名单详情及用户群信息缓存信息开始]");
        redisTemplate.delete(RedidKeyConstant.BLACK_LIST_REDIS_KEY);
        // 不存在key 或者过期时
        List<RosterBlackEntity> rosterBlackEntityList = rosterMapper.getBlackListAll();
        Map<Long, List<RosterBlackEntity>> blackListMap = rosterBlackEntityList.stream().collect(Collectors.groupingBy(RosterBlackEntity::getRosterId));
        blackListMap.forEach((key, value) -> {
            redisTemplate.opsForHash().put(RedidKeyConstant.BLACK_LIST_REDIS_KEY, String.valueOf(key), JSONUtil.toJsonStr(value));
        });
        //存储
        redisTemplate.boundValueOps(RedidKeyConstant.BLACK_LIST_REDIS_KEY).expire(cacheTime, TimeUnit.MILLISECONDS);
        redisTemplate.delete(RedidKeyConstant.WHITE_LIST_REDIS_KEY);
        List<RosterWhiteEntity> rosterWhiteEntityList = rosterMapper.getWhiteListAll();
        Map<Long, List<RosterWhiteEntity>> whiteListMap = rosterWhiteEntityList.stream().collect(Collectors.groupingBy(RosterWhiteEntity::getRosterId));
        ;
        //存储
        whiteListMap.forEach((key, value) -> {
            redisTemplate.opsForHash().put(RedidKeyConstant.WHITE_LIST_REDIS_KEY, String.valueOf(key), JSONUtil.toJsonStr(value));
        });
        redisTemplate.boundValueOps(RedidKeyConstant.WHITE_LIST_REDIS_KEY).expire(cacheTime, TimeUnit.MILLISECONDS);
        redisTemplate.delete(RedidKeyConstant.BANNER_GROUP_STATUS);
        List<UserGroupBo> userGroupBos = crowdMapper.selectStaticCustomerByAll();
        Map<String, Integer> userGroupBoMap = userGroupBos.stream().collect(Collectors.toMap(UserGroupBo::getGroupId, UserGroupBo::getIsStaticCustomer));
        //存储
        userGroupBoMap.forEach((key, value) -> {
            redisTemplate.opsForHash().put(RedidKeyConstant.BANNER_GROUP_STATUS, String.valueOf(key), value.toString());
        });
        redisTemplate.boundValueOps(RedidKeyConstant.BANNER_GROUP_STATUS).expire(cacheTime, TimeUnit.MILLISECONDS);
        log.info("[初始化黑白名单详情及用户群信息缓存信息成功]");
    }



}
